package com.nstar.uecenter.business
{
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	/**
	 * @description	:	远程对象管理模板
	 * @auth		:	zhucm
	 * @date		:	2009.9
	 */ 
	public class TemplateManager
	{
		private static var inst:TemplateManager;
		
		//回调方法
		private var callback:Function;
		
		private function TemplateManager()
		{
		}
		
		//获取实例,singleton模式
		public static function getInstance(callback:Function=null):TemplateManager{
			if(inst){
				return inst;
			}
			
			inst = new TemplateManager();
			inst.callback = callback;//设置回调方法
			return inst;
		}
		
		//取得远程对象
		private function getService(destination:String="helloWorld"):RemoteObject{
			var service:RemoteObject = new RemoteObject();
			service.destination = destination;
			//设置调用正常与异常的处理方法
			service.addEventListener(ResultEvent.RESULT,resultHandler);
			service.addEventListener(FaultEvent.FAULT,faultHandler);
			//显示"忙"鼠标
			service.showBusyCursor = true;
			return service;
		}
		
		//查询方法示例
		public function find(name:String):void{
			var service:RemoteObject = this.getService();
			service.find(name);
		}
		
		//正确调用返回处理
		private function resultHandler(event:ResultEvent):void{
			if(callback){
				callback.call(this,event.result);
			}
		}
		
		//后台调用异常
		private function faultHandler(event:FaultEvent):void{
			throw new Error(event.message);
		}
		

	}
}